package com.fyh.hiring.talent.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.fyh.hiring.common.DBObject;
import com.fyh.hiring.common.util.JDBCUtil;
import com.fyh.hiring.talent.entity.TalentEntity;

/**
 * 登录的数据库操作
 * 
 * @author 付元辉.
 *
 * @since2016年2月28日
 */
public class TalentDao {

	public TalentEntity login(String code, String password) throws SQLException {
		TalentEntity talent = null;
		DBObject dbObject = null;
		try {
			dbObject = JDBCUtil.executeQuery(
					"SELECT * FROM talent WHERE code=? AND password=?", code,
					password);
			ResultSet resultSet = dbObject.getResultSet();

			while (resultSet.next()) {
				talent = new TalentEntity();
                talent.setCode(resultSet.getString("code"));
                talent.setPassWord(resultSet.getString("passWord"));
				talent.setName(resultSet.getString("name"));
				talent.setPhone(resultSet.getString("phone"));
				talent.setId(resultSet.getInt("id"));
				talent.setAge(resultSet.getInt("age"));
				talent.setGender(resultSet.getInt("gender"));
				talent.setEmail(resultSet.getString("email"));
				talent.setHobby(resultSet.getString("hobby"));

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(dbObject);
		}
		// 最后在吧实体返回出去
		return talent;

	}

	/**
	 * 注册的数据库操作
	 * 
	 * @param code
	 * @param password
	 * @return
	 */
	public int register(TalentEntity talent) {

		int row = 0;
		String sql = "INSERT INTO talent values(NULL,?,?,?,?,?,?,?,?)";
		// 这里参数过多，我要使用list
		try {
			List<Object> params = new ArrayList<>();
			params.add(talent.getCode());
			params.add(talent.getPassWord());
			params.add(talent.getName());
			params.add(talent.getPhone());
			params.add(talent.getEmail());
			params.add(talent.getAge());
			params.add(talent.getGender());
			params.add(talent.getHobby());
			row = JDBCUtil.executeUpdate(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}

	/**
	 * 修改个人资料的数据库操作
	 * @param talent
	 * @return
	 */
	public int update(TalentEntity talent) {
		int row = 0;
		String sql = "UPDATE talent SET name=?,passWord=?,phone=?,email=?,hobby=?,age=? WHERE id =?";
		// 这里参数过多，我要使用list
		try {
			List<Object> params = new ArrayList<>();
			params.add(talent.getName());
			params.add(talent.getPassWord());
			params.add(talent.getPhone());
			params.add(talent.getEmail());
			params.add(talent.getHobby());
			params.add(talent.getAge());
//			params.add(talent.getGender());
		    params.add(talent.getId());
			row = JDBCUtil.executeUpdate(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}
}
